package com.cy.pj.sys.service.serviceImpl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.AdminRoleDao;
import com.cy.pj.sys.pojo.Role;
import com.cy.pj.sys.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {
    @Autowired
    private AdminRoleDao adminRoleDao;

    @Override
    public List<Role> selectAssignedRole(Integer adminId) {
        if(adminId==null){
            throw new ServiceException("系统故障");
        }
        List<Role> roles = adminRoleDao.selectAssignedRole(adminId);
        if(roles.isEmpty()||roles==null){
            throw  new ServiceException("没有为admin分配角色");
        }
        return roles;
    }

    @Override
    public List<Role> selectUnAssignedRole(Integer adminId) {
        if(adminId==null){
            throw new ServiceException("系统故障");
        }
        List<Role> roles = adminRoleDao.selectUnAssignedRole(adminId);
        if(roles.isEmpty()||roles==null){
            throw  new ServiceException("admin分配了所有角色");
        }

        return roles;
    }

    @Override
    public int insertAssignedRole(Integer adminId, Integer[] roleIds) {
        if(adminId==null){
            throw new ServiceException("系统故障");
        }
        if(roleIds==null||roleIds.length==0){
            throw new ServiceException("请为用户至少选择一个角色");
        }
        int rows = adminRoleDao.insertAssignedRole(adminId, roleIds);
        if(rows==0){
            throw new ServiceException("可能该角色已经被删除了");
        }
        return rows;
    }

    @Override
    public int deleteAssignRole(Integer adminId, Integer[] roleIds) {
        if(adminId==null){
            throw new ServiceException("系统故障");
        }
        if(roleIds==null||roleIds.length==0){
            throw new ServiceException("请为用户至少选择一个角色");
        }
        int rows = adminRoleDao.deleteAssignRole(adminId, roleIds);
        if(rows==0){
            throw new ServiceException("可能该角色已经被删除了");
        }
        return rows;
    }
}
